﻿using Xejen.Ipc.Framework.Flows;
using Xejen.Ipc.Framework.Steps;
using Xejen.Logger;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Xejen.Ipc.Framework
{
    /// <inheritdoc cref="IIpcHost"/>
    public class IpcHost : IIpcHost
    {
        /// <inheritdoc/>
        public IpcHostConfig Config { get; set; }

        /// <inheritdoc cref="IpcHost"/>
        protected IpcHost() { }

        /// <summary>
        /// 创建主机
        /// </summary>
        /// <param name="loggerManager"><inheritdoc cref="ILoggerManager" path="/summary"/></param>
        /// <returns></returns>
        public static IIpcHost CreateHost(ILoggerManager loggerManager)
        {
            Check.NotNull(loggerManager, nameof(loggerManager));

            IpcHostConfig config = new IpcHostConfig();
            HostRuntime.IpcHostOptions = config;
            HostRuntime.LoggerManager = loggerManager;

            var host = new IpcHost()
            {
                Config = config
            };

            var logger = loggerManager.CreateLogger<IpcHost>();
            logger.LogInformation("创建工业流程控制主机");

            return host;
        }

        /// <summary>
        /// 创建主机
        /// </summary>
        /// <param name="config"><inheritdoc cref="IpcHostConfig" path="/summary"/></param>
        /// <param name="loggerManager"><inheritdoc cref="ILoggerManager" path="/summary"/></param>
        /// <returns></returns>
        public static IIpcHost CreateHost(IpcHostConfig config, ILoggerManager loggerManager)
        {
            Check.NotNull(config, nameof(config));
            Check.NotNull(loggerManager, nameof(loggerManager));

            HostRuntime.IpcHostOptions = config;
            HostRuntime.LoggerManager = loggerManager;

            var host = new IpcHost() { Config = config };

            var logger = loggerManager.CreateLogger<IpcHost>();
            string message = "创建工业流程控制主机";
            Debug.WriteLine(message);
            logger.LogInformation(message);

            return host;
        }
    }
}
